<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}歌曲`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.rowData"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="歌曲名称" prop="name">
        <el-input v-model="drawerProps.rowData.name" placeholder="请输入歌曲名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="歌手名称" prop="singer">
        <el-input v-model="drawerProps.rowData.singer" placeholder="请输入歌手名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="歌曲链接" prop="url">
        <el-input v-model="drawerProps.rowData.url" placeholder="请输入歌曲链接" clearable></el-input>
      </el-form-item>
      <el-form-item label="歌曲封面" prop="cover">
        <UploadImg v-model:imageUrl="drawerProps.rowData.cover as string" width="80px" height="80px" :file-size="3">
          <template #empty>
            <el-icon><Plus /></el-icon>
            <span>请上传封面</span>
          </template>
          <template #tip> 图片大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="drawerProps.rowData.sort" :min="0" placeholder="请输入排序" style="width:180px" clearable />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="drawerProps.rowData.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Song } from "@/interface/api";
import UploadImg from "@/components/Upload/Img.vue";
import { Plus } from "@element-plus/icons-vue";
import { Recordable } from "@/interface/types.ts";
import {getAudioDuration} from "@/helper";

const rules = reactive<Recordable>({
  name: [{ required: true, message: "请输入歌曲名称" }],
  singer: [{ required: true, message: "请输入歌手名称" }],
  url: [{ required: true, message: "请输入歌曲链接" }],
  status: [{ required: true, message: "歌曲状态不能为空" }],
});

interface DrawerProps {
  title: string;
  isView: boolean;
  rowData: Partial<Song>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  rowData: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const duration = await getAudioDuration(drawerProps.value.rowData.url as string);
      drawerProps.value.rowData.duration = Math.floor(duration * 1000) as number;
      await drawerProps.value.api!(drawerProps.value.rowData);
      ElMessage.success({ message: `${drawerProps.value.title}歌曲成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
